package com.example.remotecontrol.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.example.remotecontrol.data.repository.DeviceRepository
import com.example.remotecontrol.data.repository.SceneRepository
import com.example.remotecontrol.data.repository.SettingsRepository
import com.example.remotecontrol.util.BluetoothManager
import com.example.remotecontrol.util.IRManager
import com.example.remotecontrol.util.WiFiManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import javax.inject.Qualifier
import javax.inject.Singleton

// 为应用创建DataStore实例
private val Context.deviceDataStore by preferencesDataStore(name = "devices")
private val Context.sceneDataStore by preferencesDataStore(name = "scenes")
private val Context.settingsDataStore by preferencesDataStore(name = "settings")

/**
 * 依赖注入模块
 * 提供应用所需的所有依赖实例
 */
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    
    /**
     * 提供设备数据存储
     */
    @Provides
    @Singleton
    fun provideDeviceDataStore(@ApplicationContext context: Context): DataStore<Preferences> {
        return context.deviceDataStore
    }
    
    /**
     * 提供场景数据存储
     */
    @Provides
    @Singleton
    fun provideSceneDataStore(@ApplicationContext context: Context): DataStore<Preferences> {
        return context.sceneDataStore
    }
    
    /**
     * 提供设置数据存储
     */
    @Provides
    @Singleton
    fun provideSettingsDataStore(@ApplicationContext context: Context): DataStore<Preferences> {
        return context.settingsDataStore
    }
    
    /**
     * 提供设备仓库
     */
    @Provides
    @Singleton
    fun provideDeviceRepository(
        deviceDataStore: DataStore<Preferences>
    ): DeviceRepository {
        return DeviceRepository(deviceDataStore)
    }
    
    /**
     * 提供场景仓库
     */
    @Provides
    @Singleton
    fun provideSceneRepository(
        sceneDataStore: DataStore<Preferences>,
        deviceRepository: DeviceRepository
    ): SceneRepository {
        return SceneRepository(sceneDataStore, deviceRepository)
    }
    
    /**
     * 提供设置仓库
     */
    @Provides
    @Singleton
    fun provideSettingsRepository(
        settingsDataStore: DataStore<Preferences>
    ): SettingsRepository {
        return SettingsRepository(settingsDataStore)
    }
    
    /**
     * 提供红外管理器
     */
    @Provides
    @Singleton
    fun provideIRManager(@ApplicationContext context: Context): IRManager {
        return IRManager(context)
    }
    
    /**
     * 提供蓝牙管理器
     */
    @Provides
    @Singleton
    fun provideBluetoothManager(@ApplicationContext context: Context): BluetoothManager {
        return BluetoothManager(context)
    }
    
    /**
     * 提供WiFi管理器
     */
    @Provides
    @Singleton
    fun provideWiFiManager(@ApplicationContext context: Context): WiFiManager {
        return WiFiManager(context)
    }
    
    /**
     * 提供IO调度器
     */
    @IoDispatcher
    @Provides
    fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
    
    /**
     * 提供默认调度器
     */
    @DefaultDispatcher
    @Provides
    fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
    
    /**
     * 提供主线程调度器
     */
    @MainDispatcher
    @Provides
    fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
}

/**
 * IO调度器限定符
 */
@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class IoDispatcher

/**
 * 默认调度器限定符
 */
@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class DefaultDispatcher

/**
 * 主线程调度器限定符
 */
@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class MainDispatcher 